﻿using COSXML.Auth;
using COSXML.Transfer;
using COSXML;
using COSXML;
using COSXML.Model.Object;
using COSXML.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Threading.Tasks;
using COSXML.Model.Object;
using COSXML.Model.Tag;
using App.WebAPI.Utilty.ApiResult;
using App.IService;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using COSXML.Common;

namespace WebAPI.Controllers
{
    [Route("[controller]/[Action]")]
    [ApiController]
    [Authorize]

    public class UploadController : ControllerBase
    {
        private readonly ILogger<AccountInfoController> _logger;
        private readonly CosXml _cosClient;
        private readonly IUserInfoService _iUserInfoService;

        public UploadController(ILogger<AccountInfoController> logger, CosXml cosClient, IUserInfoService iUserIofoService)
        {
            _logger = logger;
            _cosClient = cosClient;
            _iUserInfoService = iUserIofoService;
        }
        [HttpPost]
        public async Task<ApiResult> UploadAvatar(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return ApiResultHelper.Error("文件不能为空");
            }
            string id = User.FindFirst(ClaimTypes.Name).Value;

            var bucketName = "mengblog-1305308047";
            var region = "ap-guangzhou";
            var timestamp = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds(); string timestampString = timestamp.ToString();
            var extension = Path.GetExtension(file.FileName);
            var key = $"images/{timestampString}{extension}";

            using var stream = new MemoryStream();
            await file.CopyToAsync(stream);
            // 创建PutObjectRequest对象，并设置自定义的HTTP头
            var putRequest = new PutObjectRequest(bucketName, key, stream);
            putRequest.SetRequestHeader("Content-Disposition", "inline");
            putRequest.SetCosACL(CosACL.PublicRead);
            _cosClient.PutObject(putRequest);

            var url = _cosClient.GetObjectUrl(bucketName, key);
            var userAvatar = await _iUserInfoService.FindAsync(id);
            userAvatar.AvatarImage = url;
            userAvatar.UpdateAt = DateTime.Now;
            bool b = await _iUserInfoService.EditAsync(userAvatar);
            if (!b) return ApiResultHelper.Error("修改失败，服务器异常");
            return ApiResultHelper.Success(userAvatar.AvatarImage, b);
        }
        [HttpPost]
        public async Task<ApiResult> UploadImg(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return ApiResultHelper.Error("文件不能为空");
            }
            var bucketName = "mengblog-1305308047";
            var region = "ap-guangzhou";
            var timestamp = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds(); string timestampString = timestamp.ToString();
            var extension = Path.GetExtension(file.FileName);
            var key = $"images/{timestampString}{extension}";


            using var stream = new MemoryStream();
            await file.CopyToAsync(stream);
            // 创建PutObjectRequest对象，并设置自定义的HTTP头
            var putRequest = new PutObjectRequest(bucketName, key, stream);
            putRequest.SetRequestHeader("Content-Disposition", "inline");
            putRequest.SetCosACL(CosACL.PublicRead);
            _cosClient.PutObject(putRequest);

            var url = _cosClient.GetObjectUrl(bucketName, key);
            return ApiResultHelper.Success(url, true);
        }
    }
}